<script setup>
import { useSubjectStore } from '@/stores'
import { ref } from 'vue'
const subjectStore = useSubjectStore()
const isrecrudescence = ref(false)
const week = ref(0)
const addrecrudescence = () => {
  subjectStore
    .addRecrudescence({
      id: subjectStore.id,
      week: week.value
    })
    .then((code) => {
      if (code) {
        subjectStore.addFactors({
          id: subjectStore.id,
          week: week.value
        })
        subjectStore.recrudescence[subjectStore.recrudescence.length] = {
          id: subjectStore.id,
          week: week.value
        }
        ElMessage.success('成功')
      } else {
        ElMessage.error('失败')
      }
    })
}
const getfactors = (week) => {
  for (let factors in subjectStore.factors) {
    if (subjectStore.factors[factors].week === week) {
      return factors
    }
  }
  return 0
}
const setRecrudescenceFactors = (data, i) => {
  subjectStore.setFactors(subjectStore.factors[i])
  subjectStore.setRecrudescence(data).then((code) => {
    if (code) {
      ElMessage.success('成功')
    } else {
      ElMessage.error('失败')
    }
  })
}
const options = [
  {
    value: 0,
    label: '继续随访',
    label1: '否'
  },
  {
    value: 1,
    label: '结束随访',
    label1: '是'
  }
]
</script>
<template>
  <el-table v-if="isrecrudescence" :data="[1]" :style="{ width: '100%' }">
    <el-table-column label="时间">
      <el-input-number v-model="week" :min="0" :max="15" />
    </el-table-column>
    <el-table-column label="编辑">
      <el-button @click="addrecrudescence">添加</el-button>
    </el-table-column>
  </el-table>
  <el-table :data="subjectStore.recrudescence" :style="{ width: '100%' }">
    <el-table-column prop="week" label="时间" width="80" />
    <el-table-column prop="date" label="时间" width="80">
      <template #default="scope">
        <el-date-picker
          v-model="scope.row.date"
          type="date"
          placeholder="日期"
        />
      </template>
    </el-table-column>
    <el-table-column label="复发评定情况" width="130">
      <template #default="scope">
        <el-select v-model="scope.row.finishFollow_up" placeholder="Select">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </template>
    </el-table-column>
    <el-table-column label="是否复发" width="130">
      <template #default="scope">
        <el-select v-model="scope.row.isRecrudescence" placeholder="Select">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label1"
            :value="item.value"
          />
        </el-select>
      </template>
    </el-table-column>
    <el-table-column label="诱发或加重因素">
      <template #default="scope">
        <template v-if="scope.row.isRecrudescence">
          <el-checkbox
            v-model="subjectStore.factors[getfactors(scope.row.week)].fatigue"
            label="疲劳"
          />
          <el-checkbox
            v-model="subjectStore.factors[getfactors(scope.row.week)].diet"
            label="饮食"
          />
          <el-checkbox
            v-model="subjectStore.factors[getfactors(scope.row.week)].infect"
            label="感染"
          />
          <el-checkbox
            v-model="
              subjectStore.factors[getfactors(scope.row.week)].menstruation
            "
            label="月经"
          />
          <el-checkbox
            v-model="subjectStore.factors[getfactors(scope.row.week)].medicine"
            label="药物"
          />
          <el-checkbox
            v-model="subjectStore.factors[getfactors(scope.row.week)].season"
            label="季节变化"
          />
          <el-checkbox
            v-model="
              subjectStore.factors[getfactors(scope.row.week)].traumaSurgery
            "
            label="外伤手术"
          />
          <el-checkbox
            v-model="subjectStore.factors[getfactors(scope.row.week)].spirit"
            label="精神因素"
          />
          <el-checkbox
            v-model="subjectStore.factors[getfactors(scope.row.week)].outer"
            label="其他"
          />
        </template>
      </template>
    </el-table-column>
    <el-table-column width="90">
      <template #header>
        <el-button
          @click="
            () => {
              isrecrudescence === true
                ? (isrecrudescence = false)
                : (isrecrudescence = true)
            }
          "
        >
          添加
        </el-button>
      </template>
      <template #default="scope">
        <el-button
          @click="
            setRecrudescenceFactors(scope.row, getfactors(scope.row.week))
          "
        >
          确定
        </el-button>
      </template>
    </el-table-column>
  </el-table>
</template>
